package com.weigee.ktv.p.frag;

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.l.d.app.library.util.ToastUtil;
import com.weigee.ktv.m.retrofit.RetrofitUtil;
import com.weigee.ktv.p.TaskDetailContract;
import com.weigee.ktv.p.TaskDetailContract.SupermarketChooseGoodsPresenter;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by linde on 2017/5/8.
 * <p/>
 */
public class SupermarketChooseGoodsPresenterImpl implements SupermarketChooseGoodsPresenter
{
    private final TaskDetailContract.SupermarketChooseGoodsView view;

    public SupermarketChooseGoodsPresenterImpl(TaskDetailContract.SupermarketChooseGoodsView view)
    {
        this.view = view;
    }

    @Override
    public void onCreated(Bundle savedInstanceState)
    {

    }

    @Override
    public void onViewCreated(Bundle savedInstanceState)
    {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        onLoadGoodsTypeData();
        onLoadListData(false);
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }
    private void onLoadGoodsTypeData()
    {
        RetrofitUtil.INSTANCE.getApiService().supermarketChooseGoodsLeftData().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                JSONArray array = null;
                try
                {
                    final String respStr = response.body().string();
                    if (!TextUtils.isEmpty(respStr))
                    {
                        array = JSONArray.parseArray(respStr);
                    }
                    view.setGoodsTypeData(array);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
                ToastUtil.makeTest(view.getMyContext(), "加载失败");
            }
        });
    }

    @Override
    public void onLoadData(Object... objects)
    {

    }

    @Override
    public void onLoadListData(final boolean moreData)
    {
        RetrofitUtil.INSTANCE.getApiService().supermarketChooseGoodsRightData("http://ktvadmin.weigee.net/api/market/products?categoryId=3&shopId=1").enqueue(new Callback<ResponseBody>()
//        RetrofitUtil.INSTANCE.getApiService().supermarketChooseGoodsRightData(3,1).enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                JSONArray array = null;
                try
                {
                    final String respStr = response.body().string();
                    if (!TextUtils.isEmpty(respStr))
                    {
                        array = JSONArray.parseArray(respStr);
                    }
                    view.setGoodsDetailData(array, moreData);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
                ToastUtil.makeTest(view.getMyContext(), "加载失败");
            }
        });
    }
}
